//
//  AppDelegate+AppService.m
//  ZDSC
//
//  Created by ZDTX on 2021/5/14.
//

#import "AppDelegate+AppService.h"

@implementation AppDelegate (AppService)

/*
 
 #ifdef NSFoundationVersionNumber_iOS_9_x_Max
 #import <UserNotifications/UserNotifications.h>
 #endif
 
 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
     // Override point for customization after application launch.
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
     self.window.backgroundColor = [UIColor whiteColor];
     UIViewController *vc;
     if (![[NSUserDefaults standardUserDefaults] boolForKey:@"kLoginFlag"]) {
         vc = [[ZYCNavigationController alloc] initWithRootViewController:[[ZYCLoginViewController alloc] init]];
     }else{
         vc = [[ZYCMainViewController alloc] init];
     }
     self.window.rootViewController = vc;
     [self.window makeKeyAndVisible];
     
     [[UINavigationBar appearance] setBackgroundImage:[[UIImage alloc] init] forBarMetrics:UIBarMetricsDefault];
     [[UINavigationBar appearance] setShadowImage:[[UIImage alloc] init]];
     
     //强制关闭深色模式
     #if defined (__IPHONE_13_0) && __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_13_0
         if(@available(iOS 13.0,*)){
             self.window.overrideUserInterfaceStyle = UIUserInterfaceStyleLight;
         }
     #endif
     [self configIQKeyboardManager];
     
     // 监控网络
 //    [self monitorNetWorkState];
     

     
 //    [[TUIKit sharedInstance] setupWithAppId:SDKAPPID logLevel:V2TIM_LOG_NONE];
     
     //
 //    [self configRemoteNotofication:application];
     

     
 //    TUIKitConfig *config = [TUIKitConfig defaultConfig];
 //    config.defaultAvatarImage = [UIImage imageNamed:@"Doge"];
     
     return YES;
 }

 #pragma mark - function 2 iOS 10之前以前的用户
 - (void)application:(UIApplication *)application didReceiveRemoteNotification:(NSDictionary *)userInfo fetchCompletionHandler:(void (^)(UIBackgroundFetchResult))completionHandler {
     NSLog(@"push notification did receive remote notification:%@", userInfo);
     completionHandler(UIBackgroundFetchResultNewData);
 }




 #pragma mark - function 3 iOS10及以后的用户
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center
        willPresentNotification:(UNNotification *)notification withCompletionHandler:(void (^)(UNNotificationPresentationOptions options))completionHandler{
     NSLog(@"push notification did receive remote notification:%@",notification.request.content.userInfo);
     // 需要执行这个方法，选择是否提醒用户，有Badge、Sound、Alert三种类型可以设置,决定是否再显示此通知来提醒用户
     completionHandler(UNNotificationPresentationOptionBadge |UNNotificationPresentationOptionSound | UNNotificationPresentationOptionAlert);
 }

 #pragma mark - function 4 iOS10及以后的用户
 - (void)userNotificationCenter:(UNUserNotificationCenter *)center didReceiveNotificationResponse:(UNNotificationResponse *)response withCompletionHandler:(void(^)(void))completionHandler {
     NSLog(@"push notification did receive remote notification:%@",response.notification.request.content.userInfo);
 }




 - (void)configRemoteNotofication:(UIApplication *)application{
     //iOS10中，改进了推送的代理方法，增加了3DTouch效果
     //https://www.jianshu.com/p/2c8cf1ccf625
     if (@available(iOS 10.0, *)) {//iOS 10以上版本
         [[UNUserNotificationCenter currentNotificationCenter] requestAuthorizationWithOptions:(UNAuthorizationOptionBadge|UNAuthorizationOptionSound|UNAuthorizationOptionAlert) completionHandler:^(BOOL granted, NSError * _Nullable error) {
             if (!error && granted) {
                 //用户点击允许
                 NSLog(@"PushNotification====注册成功");
             }else{
                 //用户点击不允许
                 NSLog(@"PushNotification====注册失败");
             }
         }];
         UNNotificationCategory* generalCategory = [UNNotificationCategory
                                                    categoryWithIdentifier:@"GENERAL"
                                                    actions:@[]
                                                    intentIdentifiers:@[]
                                                    options:UNNotificationCategoryOptionCustomDismissAction];
         UNNotificationAction* snoozeAction = [UNNotificationAction
                                               actionWithIdentifier:@"SNOOZE_ACTION"
                                               title:@"Snooze"
                                               options:UNNotificationActionOptionAuthenticationRequired];
         UNNotificationAction* stopAction = [UNNotificationAction
                                             actionWithIdentifier:@"STOP_ACTION"
                                             title:@"Stop"
                                             options:UNNotificationActionOptionDestructive];
         UNNotificationAction* forAction = [UNNotificationAction
                                            actionWithIdentifier:@"FOR_ACTION"
                                            title:@"forAction"
                                            options:UNNotificationActionOptionForeground];
         UNNotificationCategory* expiredCategory = [UNNotificationCategory
                                                    categoryWithIdentifier:@"TIMER_EXPIRED"
                                                    actions:@[snoozeAction, stopAction,forAction]
                                                    intentIdentifiers:@[]
                                                    options:UNNotificationCategoryOptionNone];
         UNUserNotificationCenter* center = [UNUserNotificationCenter currentNotificationCenter];
         [center setDelegate:self];
         [center setNotificationCategories:[NSSet setWithObjects:generalCategory, expiredCategory,
                                            nil]];


     } else if(@available(iOS 8.0, *)){//iOS 8 - iOS 9系统
         UIRemoteNotificationType type = UIRemoteNotificationTypeAlert | UIRemoteNotificationTypeBadge | UIRemoteNotificationTypeSound;
 //        application registerUserNotificationSettings:<#(nonnull UIUserNotificationSettings *)#>
         [application registerForRemoteNotificationTypes:type];
     }
     //注册远端消息通知获取device token
     [application registerForRemoteNotifications];

 }

 #pragma mark - 授权申请token回调
 - (void)application:(UIApplication *)application didRegisterForRemoteNotificationsWithDeviceToken:(NSData *)deviceToken{
     //保存deviceToken
     NSLog(@"deviceTokens:%@",deviceToken);
     NSData  *apnsToken = [NSData dataWithData:deviceToken];
     NSString *tokenString = [self getHexStringForData:apnsToken];
     NSLog(@"My token = %@", tokenString);
 //1eb58fab b34f6969 5e9e47ec f279793c 7e54b72b 90f3f7b3 c30c495f e36cf49e
 }

 - (NSString *)getHexStringForData:(NSData *)data {
     NSUInteger length = [data length];
     char *chars = (char *)[data bytes];
     NSMutableString *hexString = [[NSMutableString alloc] init];
     for (NSUInteger i = 0; i < length; i++) {
         [hexString appendString:[NSString stringWithFormat:@"%0.2hhx", chars[i]]];
     }
     return hexString;
 }

 //token获取失败
 - (void)application:(UIApplication *)application didFailToRegisterForRemoteNotificationsWithError:(NSError *)error {
     NSLog(@"did Fail To Register For Remote Notifications With Error: %@", error);
 }
 */
@end
